﻿using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using AnyOffice.BIZ;
using log4net;
using System.Reflection;
namespace AnyOffice.general.hrms.manage
{
    public partial class _new : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected string strTemp;

        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ConfigManager manager = new ConfigManager();
            if (this.filePhoto.PostedFile.ContentLength > 0)
            {
                if (this.filePhoto.PostedFile.ContentLength > manager.ImageUploadMaxSize)
                {
                    this.litMessage.Text = base.ShowPrompt("照片大小超过500k！");
                    return;
                }
                if (!this.IsImage(this.filePhoto.PostedFile.ContentType))
                {
                    this.litMessage.Text = base.ShowPrompt("照片格式不正确！");
                    return;
                }
            }
            string attachmentId = "";
            try
            {
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 10;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            HrmsDao dao2 = new HrmsDao();
            HrmsData data2 = new HrmsData();
            if (this.UserId.Value != string.Empty)
            {
                data2.UserId = this.UserId.Value;
            }
            data2.UserName = this.txtUserName.Text;
            if (this.txtNumber.Value != string.Empty)
            {
                data2.Number = this.txtNumber.Value;
            }
            data2.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
            data2.Sex = Convert.ToBoolean(this.selectSex.Value);
            if (this.txtBirthday.Value != string.Empty)
            {
                data2.Birthday = Convert.ToDateTime(this.txtBirthday.Value);
            }
            if (this.filePhoto.PostedFile.ContentLength > 0)
            {
                int contentLength = this.filePhoto.PostedFile.ContentLength;
                byte[] buffer = new byte[contentLength];
                this.filePhoto.PostedFile.InputStream.Read(buffer, 0, contentLength);
                data2.Photo = buffer;
            }
            if (this.txtNation.Value != string.Empty)
            {
                data2.Nation = this.txtNation.Value;
            }
            if (this.txtCardNo.Value != string.Empty)
            {
                data2.CardNo = this.txtCardNo.Value;
            }
            if (this.selectMarry.Value != string.Empty)
            {
                data2.Marry = this.selectMarry.Value;
            }
            if (this.selectPolitics.Value != string.Empty)
            {
                data2.Politics = this.selectPolitics.Value;
            }
            if (this.txtNativePlace.Value != string.Empty)
            {
                data2.NativePlace = this.txtNativePlace.Value;
            }
            if (this.txtHukouPlace.Value != string.Empty)
            {
                data2.HukouPlace = this.txtHukouPlace.Value;
            }
            if (this.selectEducation.Value != string.Empty)
            {
                data2.Education = this.selectEducation.Value;
            }
            if (this.txtTechPost.Value != string.Empty)
            {
                data2.TechPost = this.txtTechPost.Value;
            }
            if (this.txtSchool.Value != string.Empty)
            {
                data2.School = this.txtSchool.Value;
            }
            if (this.txtSpeciallity.Value != string.Empty)
            {
                data2.Speciallity = this.txtSpeciallity.Value;
            }
            if (this.txtWorkDate.Value != string.Empty)
            {
                data2.WorkDate = Convert.ToDateTime(this.txtWorkDate.Value);
            }
            if (this.txtJoinDate.Value != string.Empty)
            {
                data2.JoinDate = Convert.ToDateTime(this.txtJoinDate.Value);
            }
            if (this.txtContractType.Value != string.Empty)
            {
                data2.ContractType = this.txtContractType.Value;
            }
            if (this.txtPost.Value != string.Empty)
            {
                data2.Post = this.txtPost.Value;
            }
            if (this.txtHomeTel.Value != string.Empty)
            {
                data2.HomeTel = this.txtHomeTel.Value;
            }
            if (this.txtEmail.Value != string.Empty)
            {
                data2.Email = this.txtEmail.Value;
            }
            if (this.txtHomeAddr.Value != string.Empty)
            {
                data2.HomeAddr = this.txtHomeAddr.Value;
            }
            if (this.txtContractDate1.Value != string.Empty)
            {
                data2.ContractDate1 = Convert.ToDateTime(this.txtContractDate1.Value);
            }
            if (this.txtContractDate2.Value != string.Empty)
            {
                data2.ContractDate2 = Convert.ToDateTime(this.txtContractDate2.Value);
            }
            if (this.txtLeaveType.Value != string.Empty)
            {
                data2.LeaveType = this.txtLeaveType.Value;
            }
            if (this.txtPostChange.Value != string.Empty)
            {
                data2.PostChange = this.txtPostChange.Value;
            }
            if (this.txtEdu.Value != string.Empty)
            {
                data2.Edu = this.txtEdu.Value;
            }
            if (this.txtResume.Value != string.Empty)
            {
                data2.Resume = this.txtResume.Value;
            }
            if (this.txtSociaty.Value != string.Empty)
            {
                data2.Sociaty = this.txtSociaty.Value;
            }
            if (this.txtReward.Value != string.Empty)
            {
                data2.Reward = this.txtReward.Value;
            }
            if (this.txtCertificate.Value != string.Empty)
            {
                data2.Certificate = this.txtCertificate.Value;
            }
            if (this.txtTrain.Value != string.Empty)
            {
                data2.Train = this.txtTrain.Value;
            }
            if (this.txtSurety.Value != string.Empty)
            {
                data2.Surety = this.txtSurety.Value;
            }
            if (this.txtContract.Value != string.Empty)
            {
                data2.Contract = this.txtContract.Value;
            }
            if (this.txtInsure.Value != string.Empty)
            {
                data2.Insure = this.txtInsure.Value;
            }
            if (this.txtBodyExamim.Value != string.Empty)
            {
                data2.BodyExamim = this.txtBodyExamim.Value;
            }
            if (this.txtMemo.Value != string.Empty)
            {
                data2.Memo = this.txtMemo.Value;
            }
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            dao2.Insert(base.Conn, data2);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("学生 " + this.txtUserName.Text + " 的档案已保存！", "dept.aspx?dept_id=" + this.ViewState["DeptId"].ToString());
        }
        private bool IsImage(string contentType)
        {
            string str;
            return (((str = contentType) != null) && (((str == "image/gif") || (str == "image/jpeg")) || (str == "image/pjpeg")));
        }
        private void LoadDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.LoadDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table2;
                ListItem item;
                int deptId = (Request.QueryString["dept_id"] != null) ? int.Parse(Request.QueryString["dept_id"].ToString()) : 0;
                this.ViewState["DeptId"] = deptId;
                if (Request.QueryString["user_id"] != null)
                {
                    string userId = Request.QueryString["user_id"].ToString();
                    DataTable table = new UsersDao().SelectByUserId(base.Conn, userId);
                    if (table.Rows.Count > 0)
                    {
                        this.litUserName.Text = "（" + table.Rows[0]["UserName"].ToString() + "） - 未建档";
                        this.UserId.Value = userId;
                        this.txtUserName.Text = table.Rows[0]["UserName"].ToString();
                        this.txtUserName.ReadOnly = true;
                        this.txtUserName.CssClass = "BigStatic";
                    }
                    table2 = new DepartmentDao().SelectByDeptId(base.Conn, deptId);
                    if (table2.Rows.Count > 0)
                    {
                        item = new ListItem("|─" + table2.Rows[0]["DeptName"].ToString(), table2.Rows[0]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                    }
                }
                else
                {
                    item = new ListItem("请选择", "");
                    this.ddlDeptId.Items.Add(item);
                    table2 = new DepartmentDao().SelectParentAll(base.Conn);
                    if (table2.Rows.Count > 0)
                    {
                        for (int i = 0; i < table2.Rows.Count; i++)
                        {
                            item = new ListItem("|─" + table2.Rows[i]["DeptName"].ToString(), table2.Rows[i]["DeptId"].ToString());
                            this.ddlDeptId.Items.Add(item);
                            this.strTemp = "";
                            this.LoadDept(ref this.ddlDeptId, int.Parse(table2.Rows[i]["DeptId"].ToString()), this.strTemp);
                        }
                    }
                    this.ddlDeptId.Items.Add(new ListItem("离校人员/外部人员", "-1"));
                    if (this.ddlDeptId.Items.FindByValue(deptId.ToString()) != null)
                    {
                        this.ddlDeptId.Items.FindByValue(deptId.ToString()).Selected = true;
                    }
                }
                ConfigManager manager = new ConfigManager();
                this.litImageMaxSize.Text = (manager.ImageUploadMaxSize / 1024).ToString();
                this.ViewState["FileNums"] = 0;
                this.ViewState["FileList"] = new ArrayList();
                this.ViewState["UniqueTempPath"] = "";
                this.ViewState["TotalSize"] = 0;
                this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }
        }

 
    }
}
